//
//  CollectionViewController.m
//  UI_APP_Project_Bean
//
//  Created by dllo on 16/6/24.
//  Copyright © 2016年 dllo. All rights reserved.
//

#import "CollectionViewController.h"
#import "ImageCollectionViewCell.h"
#import "MovieModel.h"
#import "MovieViewController.h"
#import "MovieDetiledViewController.h"

@interface CollectionViewController ()<UICollectionViewDataSource, UICollectionViewDelegate>
@property(nonatomic, retain)MovieModel *movModel;
@end

@implementation CollectionViewController

- (void)dealloc
{
    [_collectionArray release];
    [_movModel release];
    [super dealloc];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor whiteColor];
   
    [self config];
    [self creatCollentionView];
}


-(void)config{
    self.navigationItem.title = @"电影";
     NSLog(@"数组有没有%@", self.collectionArray);
    NSLog(@"hahahhahahahahahah");
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc]initWithImage:[UIImage imageNamed:@"btn_nav_list@2x.png"]style:UIBarButtonItemStylePlain target:self action:@selector(sovieAction:)];

}

-(void)sovieAction:(UIBarButtonItem *)bar{
    
    [self.navigationController  popViewControllerAnimated:YES];
    
}


-(void)creatCollentionView{
    
    UICollectionViewFlowLayout *flowLayout = [[UICollectionViewFlowLayout alloc]init];
    
    flowLayout.itemSize = CGSizeMake((CGRectGetWidth(self.view.bounds)-20) / 3, CGRectGetHeight(self.view.bounds) / 3);
    
    flowLayout.minimumInteritemSpacing = 0;
    
    UICollectionView *rainCollectionView = [[UICollectionView alloc]initWithFrame:self.view.bounds collectionViewLayout:flowLayout];
    rainCollectionView.contentInset = UIEdgeInsetsMake(0, 10, 0, 10);
    rainCollectionView.backgroundColor = [UIColor whiteColor];
    [self.view addSubview:rainCollectionView];
    
    
    rainCollectionView.delegate = self;
    rainCollectionView.dataSource = self;
    
    
    [rainCollectionView registerClass:[ImageCollectionViewCell class] forCellWithReuseIdentifier:@"cellCollection"];

    [flowLayout release];
    [rainCollectionView release];
    
    
}

-(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
    return self.collectionArray.count;
}

-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
    ImageCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"cellCollection" forIndexPath:indexPath];
    
    cell.movieViewModel = [self.collectionArray objectAtIndex:indexPath.row];
    
    return cell;
    
    
    
    
}

-(void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath{
    MovieDetiledViewController *movDetiVc = [[MovieDetiledViewController alloc]init];
    
    movDetiVc.movieModel = [self.collectionArray objectAtIndex:indexPath.row];
    
    
    [self.navigationController pushViewController:movDetiVc animated:YES];
    
    
    [movDetiVc release];
    
    
    
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
